﻿using System;
using System.Threading;
using System.Windows.Forms;

namespace com.fukefu.Components
{
    public partial class JsForm : BaseForm
    {
        private static JsForm js = null;
        private static object syncObject = new object();

        private JsForm()
        {
            InitializeComponent();
        }
        public static JsForm GetInstance()
        {
            if (js == null || js.IsDisposed)
            {
                lock (syncObject)
                {
                    if (js == null || js.IsDisposed)
                    {
                        js = new JsForm();
                    }
                }
            }
            return js;
        }
        //开始
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            new Thread(new ThreadStart(JiaMi)).Start();
        }
        //加密函数
        private void JiaMi()
        {
            string text = MyFunction.EncodeByUtf8(GetControl("textbox1", ControlValue.Text).ToString());
            string re = "";
            string code = (new Random().Next(1234567891) + 500).ToString();
            int len = code.Length;
            for (int i = 0; i < text.Length; i++)
            {
                re += ((int)text[i] ^ int.Parse(code[i % len].ToString())).ToString("x");
            }
            SetControl("textbox2", ControlValue.Text, "eval(FoxDec('" + re + "','" + code + "'))");
            SetControl("button1", ControlValue.Enable, true);
        }
        //复制结果
        private void button2_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(textBox2.Text, true);
        }
        //复制函数
        private void button3_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(@"function FoxDec(input, key)
{
    var ret = ""; 
    for(var I=0;I<input.length;I+=2)
    {
        var sig         = input.substr(I,2);
        sig             = parseInt(sig,16);
        var i           = (I/2) % key.length;
        var xor_key     = key.substr(i,1);
        sig             = sig ^ xor_key;
        if(sig==43)
        {
	        sig=32;
        }
        ret+= String.fromCharCode(sig);
    }
    return  decodeURIComponent(ret);
}", true);
        }
        //html转js
        private void button4_Click(object sender, EventArgs e)
        {

        }
    }
}
